::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::
Using Drive$
Liberty BASIC has a special variable called Drive$ which contains a string with a list of drives on the computer. This variable is filled when the program starts. It does not change if the user installs or removes drives during the program's execution. With the advent of removable drives like jump drives, removable CD drives, etc. it may be necessary to check for installed drives at different points during the run of a program.
Getting Drive Information by API
The API call to retrieve the drives installed at any point in time during execution is GetLogicalDriveStringsA . It is necessary to make this API call twice. The first time it is called, we use a very small string buffer. This causes the function to return the size needed for a string buffer to hold all of the drives. Do it like this:
'set up a small string buffer: driveString$ = space$(2) lenBuffer = len(driveString$) 'make the API call to find out how big the buffer must be calldll #kernel32, "GetLogicalDriveStringsA",_ lenBuffer as ulong,_ 'length of buffer driveString$ as ptr,_ 'string buffer will be filled by function numBytes as ulong 'returns the needed size of buffer
Creating a Buffer of the Correct Size
The required size of the buffer is returned in the numBytes variable, and we use that value to resize our string.
'resize buffer to hold the returned information: driveString$ = space$(numBytes+1) lenBuffer = len(driveString$)
Getting the Drives into a String Buffer
We can now make the API call to GetLogicalDriveStringsA. The function places a list of drives in our string buffer, and the length of the altered string is returned in numBytes. We can truncate the string using the numBytes value, like so:
'make the call again to fill the buffer with drive info calldll #kernel32, "GetLogicalDriveStringsA",_ lenBuffer as ulong,_ 'length of buffer driveString$ as ptr,_ 'string buffer will be filled by function numBytes as ulong 'length of the string returned 'the driveString$ now contains a list of drives, 'separated by null characters d$ = left$(driveString$,numBytes) print "API-returned driveString$ is ";chr$(34);d$;chr$(34)
Parsing the String Buffer
The drive letters in the returned string are separated by null characters. We can use the WORD$() function to break the string into individual drive letters. Each word in the string is evaluated in a loop until the WORD$() function returns a null string, which means we've reached the end of the string. The code to parse the string follows.
'now break into individual drive info:
print "Current drives on this machine are as follows:"
print
i = 1 'start with first drive in string
while word$(d$,i)<>""
print word$(d$,i)
i = i + 1
wend
DEMO
This small demonstration program shows the contents of the DRIVE$ variable, and then shows how to obtain drive information by API.
'Drives$ contains the installed drives when a program
'starts running. It is not refilled if drives are
'added or removed while a program is running.
'
'Use the following API method during a program's run
'to discover which drives are installed at any particular
'time.
print "Drives$ holds this information: ";Drives$
print:print
'set up a small string buffer:
driveString$ = space$(2)
lenBuffer = len(driveString$)
'make the API call to find out how big the buffer must be
calldll #kernel32, "GetLogicalDriveStringsA",_
lenBuffer as ulong,_ 'length of buffer
driveString$ as ptr,_ 'string buffer will be filled by function
numBytes as ulong 'returns the needed size of buffer
'resize buffer to hold the returned information:
driveString$ = space$(numBytes+1)
lenBuffer = len(driveString$)
'make the call again to fill the buffer with drive info
calldll #kernel32, "GetLogicalDriveStringsA",_
lenBuffer as ulong,_ 'length of buffer
driveString$ as ptr,_ 'string buffer will be filled by function
numBytes as ulong 'length of the string returned
'the driveString$ now contains a list of drives,
'separated by null characters
d$ = left$(driveString$,numBytes)
print "API-returned driveString$ is ";chr$(34);d$;chr$(34)
print:print
'now break into individual drive info:
print "Current drives on this machine are as follows:"
print
i = 1 'start with first drive in string
while word$(d$,i)<>""
print word$(d$,i)
i = i + 1
wend
print:print "End of drive listing."
end
::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::